package com.meida.base.provider.controller;

import com.meida.common.annotation.Action;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.utils.StringUtils;
import com.meida.module.admin.client.api.BaseUserRemoteApi;
import com.meida.module.admin.client.entity.BaseRole;
import com.meida.module.admin.client.model.UserInfo;
import com.meida.module.admin.provider.service.BaseAccountService;
import com.meida.module.admin.provider.service.BaseRoleService;
import com.meida.module.admin.provider.service.BaseUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户信息
 *
 * @author zyf
 */
@Api(tags = "系统用户", value = "系统用户")
@RestController
@RequestMapping("/manager/")
public class BaseUserController implements BaseUserRemoteApi {
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private BaseAccountService baseAccountService;
    @Autowired
    private BaseRoleService baseRoleService;

    /**
     * 系统分页用户列表
     *
     * @return
     */
    @ApiOperation(value = "系统用户分页列表", notes = "平台管理用户")
    @GetMapping("/user/page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return baseUserService.pageList(params);
    }


    /**
     * 获取所有用户列表
     *
     * @return
     */
    @ApiOperation(value = "获取所有用户列表", notes = "获取所有用户列表")
    @GetMapping("/user/all")
    public ResultBody<List<BaseRole>> getUserAllList() {
        return ResultBody.ok(baseUserService.findAllList());
    }

    /**
     * 添加系统用户
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "添加系统用户", notes = "添加系统用户")
    @PostMapping("/user/add")
    @Action(title = "添加")
    public ResultBody<Long> add(@RequestParam(required = false) Map params) {
        return baseUserService.add(params);
    }

    /**
     * 更新系统用户
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "更新系统用户", notes = "更新系统用户")
    @PostMapping("/user/update")
    @Action(title = "修改")
    public ResultBody updateUser(@RequestParam(required = false) Map params) {
        return baseUserService.edit(params);
    }

    /**
     * 修改用户密码
     *
     * @param userId
     * @param password
     * @return
     */
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @PostMapping("/user/update/password")
    public ResultBody updatePassword(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "password") String password
    ) {
        baseAccountService.resetPassword(userId, password);
        return ResultBody.ok();
    }

    /**
     * 用户分配角色
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @ApiOperation(value = "用户分配角色", notes = "用户分配角色")
    @PostMapping("/user/setRole")
    public ResultBody addUserRoles(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "roleIds", required = false) String roleIds
    ) {
        baseRoleService.saveUserRoles(userId, StringUtils.isNotBlank(roleIds) ? roleIds.split(",") : new String[]{});
        return ResultBody.msg("设置成功");
    }

    /**
     * 获取用户角色
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户已分配角色", notes = "获取用户已分配角色")
    @GetMapping("/user/roles")
    public ResultBody<List<BaseRole>> getUserRoles(
            @RequestParam(value = "userId") Long userId
    ) {
        return ResultBody.ok(baseRoleService.getUserRoles(userId));
    }

    /**
     * 更新用户状态
     */
    @ApiOperation(value = "用户-更新状态", notes = "更新用户状态")
    @PostMapping(value = "/user/setStatus")
    @ApiImplicitParam(name = "userId", required = true, value = "主键", paramType = "form")
    public ResultBody setStatus(@RequestParam(value = "userId") Long userId) {
        return baseUserService.setStatus(userId);
    }

    /**
     * 删除用户
     *
     * @param model
     * @return
     */
    @ApiOperation(value = "用户-删除", notes = "删除用户")
    @PostMapping(value = "/user/remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return baseUserService.delete(params);
    }


    /**
     * 获取用户详细信息
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户详细信息", notes = "获取用户详细信息")
    @Override
    public ResultBody<UserInfo> getUserInfo(@RequestParam(value = "userId") Long userId) {
        return ResultBody.ok(baseUserService.getUserWithAuthoritiesById(userId));
    }


    /**
     * 用户分配企业
     *
     * @param userId
     * @param companyIds
     * @return
     */
    @ApiOperation(value = " 用户分配企业", notes = " 用户分配企业")
    @PostMapping("/user/setCompany")
    public ResultBody saveUserCompanys(@RequestParam(value = "userId") Long userId, @RequestParam(value = "companyIds", required = false) String companyIds) {
        baseUserService.saveUserCompanys(userId, StringUtils.isNotBlank(companyIds) ? companyIds.split(",") : new String[]{});
        return ResultBody.msg("分配成功");
    }

    /**
     * 用户分配部门
     *
     * @param userId
     * @param deptIds
     * @return
     */
    @ApiOperation(value = " 用户分配部门", notes = " 用户分配部门")
    @PostMapping("/user/setDept")
    public ResultBody setDept(@RequestParam(value = "userId") Long userId, @RequestParam(value = "deptIds", required = false) String deptIds) {
        baseUserService.saveUserDepts(userId, StringUtils.isNotBlank(deptIds) ? deptIds.split(",") : new String[]{});
        return ResultBody.msg("分配成功");
    }

    @ApiOperation(value = "获取分配部门列表")
    @GetMapping(value = "user/getAuthDeptList")
    public ResultBody getAuthDeptList(@RequestParam(value = "userId") Long userId, @RequestParam(value = "organizationId") Long organizationId) {
        return baseUserService.getAuthDeptList(userId, organizationId);
    }

}
